package com.fjh.module.rbac.service.impl;

import java.util.Objects;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import com.fjh.common.constant.Constants;
import com.fjh.common.manager.AsyncManager;
import com.fjh.common.manager.factory.AsyncFactory;
import com.fjh.security.plus.starter.authentication.service.EasyAuthLogoutSuccessService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

/**
 * @author fanjh37
 */
@Service
public class EasyAuthLoginSuccessServiceImpl implements EasyAuthLogoutSuccessService {

    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse, Authentication authentication) {
        // 记录用户退出登录成功日志
        if (Objects.isNull(authentication)) {
            return;
        }
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        AsyncManager.me().execute(
            AsyncFactory.recordLogininfor(userDetails.getUsername(), Constants.LOGOUT,
                "退出成功"));
    }
}
